<?php

class Tabs_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->tableName = 'set_tabs';
    }

    function newTab($added_by) {
        $order = $this->getMaxOrder();
        $data = $_POST;
        $data['added_by'] = $added_by;
        $data['last_modified'] = date("Y-m-d H:i:s");
        $data['tab_order'] = $order + 1;
        $data['active'] = 1;
        $this->db->insert($this->tableName, $data);
    }

    function delete($tid) {
        $data['tid'] = $tid;
        $this->db->where($data);
        $this->db->delete($this->tableName);
        $this->rearrangeOrder();
    }

    function getMaxOrder() {
        $query = $this->db->query('SELECT MAX(tab_order) as tab_order FROM ' . $this->tableName . '');
        $row = $query->row_array();
        return $row['tab_order'];
    }

    function getOrder($name) {
        //get tab category
        $query = $this->db->query("SELECT cat,tab_order FROM " . $this->tableName . " WHERE url like '%" . $name . "'");
        $row = $query->row_array();
        $tab_order = $row['tab_order'];
        $cat = $row['cat'];
        //get number of active tabs order lower than this tab.
        $query = $this->db->query("SELECT count(*) as 'count' FROM " . $this->tableName . " WHERE active='1' and cat='$cat' and tab_order < '$tab_order'");
        $row = $query->row_array();
        $real_order = $row['count'];
        return $cat . "/" . $real_order;
    }

    function rearrangeOrder($sort=FALSE) {
        $sql = 'SELECT tid, tab_order  FROM ' . $this->tableName . '';
        if ($sort) {
            $sql.="  order by name";
        } else {
            $sql.="  order by tab_order";
        }
        $query = $this->db->query($sql);
        $order = 0;
        foreach ($query->result_array() as $row) {
            $this->db->query('UPDATE ' . $this->tableName . ' SET tab_order=' . $order . ' WHERE tid=' . $row['tid'] . '');
            $order++;
        }
    }

    function activate($tid) {//or deactivate
        $query = $this->db->query("SELECT active  FROM " . $this->tableName . " WHERE tid='" . $tid . "'");
        $row = $query->row_array();
        $this->db->query('UPDATE ' . $this->tableName . ' SET active=' . (($row['active'] * -1) + 1) . ' WHERE tid=' . $tid . '');
    }

    function update($tid, $cat) {
        $this->db->query('UPDATE ' . $this->tableName . ' SET cat=' . $cat . ' WHERE tid="' . $tid . '"');
    }

    function changeOrder($tid, $direction) {//$direction=UP or DOWN
        $query = $this->db->query('SELECT tab_order  FROM ' . $this->tableName . ' WHERE tid="' . $tid . '"');
        $row = $query->row_array();
        $current_order = $row['tab_order'];

        if ($direction == "UP") {
            //create a space before the prev order without selecting the current one
            $query = $this->db->query('SELECT tid, tab_order  FROM ' . $this->tableName . ' WHERE tab_order > ' . ( $current_order - 2) . '
                and tab_order!=' . $current_order . '');
        } else {
            //create a space after the next order
            $query = $this->db->query('SELECT tid, tab_order  FROM ' . $this->tableName . ' WHERE tab_order > ' . ( $current_order + 1) . '');
        }


        foreach ($query->result_array() as $row) {
            $this->db->query('UPDATE ' . $this->tableName . ' SET tab_order=' . ($row['tab_order'] + 1) . ' WHERE tid=' . $row['tid'] . '');
        }


        if ($direction == "UP") {
            //go behind the prev order
            $this->db->query('UPDATE ' . $this->tableName . ' SET tab_order=' . ($current_order - 1) . ' WHERE tid="' . $tid . '"');
//            $this->rearrangeOrder();
        } else {
            //overtake the next order
            $this->db->query('UPDATE ' . $this->tableName . ' SET tab_order=' . ($current_order + 2) . ' WHERE tid="' . $tid . '"');
        }


        $this->rearrangeOrder();
    }

    function getAll($ALL=FALSE, $cat=NULL) {
        $this->db->select('tid,added_by,name,url,,active'); //tab_order,last_modified
        if ($cat) {
            $this->db->where('cat', $cat);
        }
        if (!$ALL) {
            $this->db->where('active', 1);
        }
        $this->db->from($this->tableName);
        $this->db->order_by("tab_order", "asc");
        $query = $this->db->get();
        $result = array();
        foreach ($query->result_array() as $row) {
            $tid = $row['tid'];
            unset($row['tid']);
            $added_by = $row['added_by'];
            $row['added_by'] = "<a href='javascript:loadUrl(\"#tab_result\",\"" . base_url() . "index.php?/admin_mis/applicantProfile/$added_by\")'>" . $row['added_by'] . "</a>";
//            $row['last_modified'] = $this->functions_model->prettyDateTime($row['last_modified']);
            $row['active'] = $row['active'] == 1 ? "<a href='" . base_url() . "index.php?/admin_settings/activateTab/$tid'>
            <img src='" . base_url() . "images/turn_on.png'  onmouseover='tooltip.show(\"Active. Click to Deactivate\");'
                onmouseout='tooltip.hide();' alt='DEACTIVATE'/></a>" :
                    "<a href='" . base_url() . "index.php?/admin_settings/activateTab/$tid'>
            <img src='" . base_url() . "images/turn_off.png'  onmouseover='tooltip.show(\"Inactive. Click to Activate\");'
                onmouseout='tooltip.hide();' alt='ACTIVATE'/></a>";
            $row['links'] = "<a href='" . base_url() . "index.php?/admin_settings/changeTabOrder/$tid/UP'><img src='" . base_url() . "images/up.png'
                 onmouseover='tooltip.show(\"Move up\");'
                onmouseout='tooltip.hide();' alt='UP'/></a> |
            <a href='" . base_url() . "index.php?/admin_settings/changeTabOrder/$tid/DOWN'><img src='" . base_url() . "images/down.png'
                 onmouseover='tooltip.show(\"Move Down\");'
                onmouseout='tooltip.hide();' alt='DOWN'/></a> |
            <a href='javascript:gotoUrl(\"" . base_url() . "index.php?/admin_settings/deleteTab/$tid\",\"Are you sure to delete this tab?\")'>
            <img src='" . base_url() . "images/delete.png'
                 onmouseover='tooltip.show(\"Delete\");'
                onmouseout='tooltip.hide();' alt='DELETE'/></a> |
            <a href='javascript:updateTab($tid)'><img src='" . base_url() . "images/move.png'
                 onmouseover='tooltip.show(\"Edit\");'
                onmouseout='tooltip.hide();' alt='Edit'/></a>";
            $result[] = $row;
        }
        return $result;
    }

}

?>
